
package com.magicalcoder.youyamvc.app.goodsspec.service.impl;

import com.magicalcoder.youyamvc.app.goodsspec.dao.GoodsSpecDao;
import com.magicalcoder.youyamvc.app.goodsspec.service.GoodsSpecService;
import com.magicalcoder.youyamvc.app.model.GoodsSpec;
import com.magicalcoder.youyamvc.app.model.defined.GoodsSpecWithGoods;
import com.magicalcoder.youyamvc.app.model.defined.GoodsSpecWithGoodsSpecCategory;
import com.magicalcoder.youyamvc.core.common.utils.ListUtils;
import com.magicalcoder.youyamvc.core.common.utils.ProjectUtil;
import com.magicalcoder.youyamvc.core.common.utils.StringUtils;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.magicalcoder.youyamvc.core.common.utils.copy.Copyer;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

/**
* 代码为自动生成 Created by www.magicalcoder.com
* 如果你改变了此类 read 请将此行删除
*  欢迎加入官方QQ群:323237052
*/

@Component("goodsSpecService")
public class GoodsSpecServiceImpl implements GoodsSpecService{
    @Resource(name="goodsSpecDao")
    private GoodsSpecDao goodsSpecDao;


    @Override
    public GoodsSpec getGoodsSpec(Long id) {
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("id", id);
        return goodsSpecDao.getGoodsSpec(query);
    }

    @Override
    public GoodsSpec selectOneGoodsSpecWillThrowException(Map<String, Object> query){
        return goodsSpecDao.getGoodsSpec(query);
    }

    @Override
    public GoodsSpec selectFirstGoodsSpec(Map<String, Object> query){
        query.put("limit",1);
        List<GoodsSpec> list = goodsSpecDao.getGoodsSpecList(query);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<GoodsSpec> getGoodsSpecList(Map<String, Object> query) {
        return goodsSpecDao.getGoodsSpecList(query);
    }

    @Override
    public Integer getGoodsSpecListCount(Map<String, Object> query) {
        return goodsSpecDao.getGoodsSpecListCount(query);
    }

    @Override
    public     Long  insertGoodsSpec(GoodsSpec entity) {
        return goodsSpecDao.insertGoodsSpec(entity);
    }

    @Override
    public void updateGoodsSpec(GoodsSpec entity) {
        //校验
        goodsSpecDao.updateGoodsSpec(entity);
    }

    @Override
    public void updateGoodsSpecWithoutNull(GoodsSpec entity) {
        //校验
        goodsSpecDao.updateGoodsSpecWithoutNull(entity);
    }

    @Override
    public void updateGoodsSpecByWhereSql(Map<String,Object> entity,String whereSql) {
        if(entity==null ||entity.isEmpty()){
            throw new RuntimeException("entity不能为空");
        }
        if(StringUtils.isBlank(whereSql)){
            throw new RuntimeException("whereSql不能为空");
        }
        entity.put("whereSql",whereSql);
        //校验
        goodsSpecDao.updateGoodsSpecByWhereSql(entity);
    }

    @Override
    public void deleteGoodsSpec(Long id) {
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("id", id);
        goodsSpecDao.deleteGoodsSpec(query);
    }
    @Override
    public void deleteGoodsSpecList(Map<String,Object> entity){
        goodsSpecDao.deleteGoodsSpecList(entity);
    }

    @Override
    public void deleteGoodsSpecByWhereSql(String whereSql) {
        if(StringUtils.isBlank(whereSql)){
            throw new RuntimeException("whereSql不能为空");
        }
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("whereSql", whereSql);
        goodsSpecDao.deleteGoodsSpecByWhereSql(query);
    }

    @Override
    public List<GoodsSpecWithGoods> getGoodsSpecWithGoodsList(Map<String, Object> query){
        return goodsSpecDao.getGoodsSpecWithGoodsList(query);
    }
    @Override
    public List<GoodsSpecWithGoodsSpecCategory> getGoodsSpecWithGoodsSpecCategoryList(Map<String, Object> query){
        return goodsSpecDao.getGoodsSpecWithGoodsSpecCategoryList(query);
    }


    @Override
    public void truncateGoodsSpec() {
        goodsSpecDao.truncateGoodsSpec();
    }

    @Override
    public void batchInsertGoodsSpec(List<GoodsSpec> list) {
        //校验
        goodsSpecDao.batchInsertGoodsSpec(list);
    }

    @Override
    public void batchUpdateGoodsSpec(List<GoodsSpec> list) {
        //校验
        goodsSpecDao.batchUpdateGoodsSpec(list);
    }
    @Override
    public void batchDeleteGoodsSpec(List<Long> idList) {
        goodsSpecDao.batchDeleteGoodsSpec(idList);
    }

    @Override
    public void batchDeleteGoodsSpecList(List<GoodsSpec> entityList){
        goodsSpecDao.batchDeleteGoodsSpecList(entityList);
    }

    @Transactional
    @Override
    public void transactionImportJsonList(List<GoodsSpec> list) {
        if(list!=null && list.size()>0){
            for(GoodsSpec goodsSpec : list){
                if (goodsSpec.getId() == null) {
                    insertGoodsSpec(goodsSpec);
                } else {
                    GoodsSpec entity = getGoodsSpec(goodsSpec.getId());
                    if(entity==null){
                        insertGoodsSpec(goodsSpec);
                    }else {
                        Copyer.copy(goodsSpec, entity);
                        updateGoodsSpec(entity);
                    }
                }
            }
        }
    }


    public List<GoodsSpec> tryQueryList(String keyword,String selectValue, String foreignJavaField){
        List<GoodsSpec> list;
        if(StringUtils.isBlank(keyword) || "全部".equals(keyword)){
            return getGoodsSpecList(ProjectUtil.buildMap("limitIndex",0,"limit", 20));
        }else{
            list = searchList("specNameFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
            if(ProjectUtil.isNum(keyword)){
            list = searchList("goodsIdFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
            }
            if(ProjectUtil.isNum(keyword)){
            list = searchList("goodsSpecCategoryIdFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
            }
        }
        return new ArrayList<GoodsSpec>();
    }

    private List<GoodsSpec> searchList(String field,String keyword){
        List<GoodsSpec> list = getGoodsSpecList(ProjectUtil.buildMap(field,keyword,"limitIndex",0,"limit", 20));
        if(ListUtils.isNotBlank(list)){
            return list;
        }
        String[] keys = keyword.split("-");
        for(String key:keys){
            list = getGoodsSpecList(ProjectUtil.buildMap(field,key,"limitIndex",0,"limit", 20));
            if(ListUtils.isNotBlank(list)){
                return list;
            }
        }
        return null;
    }
}
